/*
 * Copyright (C) Antonio Ramon Morcillo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package soft.xitxarello.su.activities;

import soft.xitxarello.su.R;
import soft.xitxarello.su.data.App;
import soft.xitxarello.su.fragments.AppListFragment;
import soft.xitxarello.su.fragments.DetailFragment;
import soft.xitxarello.su.fragments.EmptyDetailFragment;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Main activity launched first in the application.
 * 
 * @see soft.xitxarello.su.activities.BaseActivity
 */
public class MainActivity extends BaseActivity implements AppListFragment.OnItemSelected {

    /**
     * Overrides a superclass method. Called when the activity is starting.
     * 
     * @param savedInstanceState A mapping from String values to various
     *            Parcelable types.
     * @see android.os.Bundle
     * @see soft.xitxarello.su.activities.BaseActivity#onCreate(Bundle
     *      savedInstanceState)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Parent method.
        super.onCreate(savedInstanceState);

        // Inflate xml Resource. /res/layout/main_activity.xml
        setContentView(R.layout.main_activity);

        // Disable the "home" button in the corner of the action bar.
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        // Actions for smartPhones and less than 10 inches tablets.
        if (isSmartPhone) {
            // Force portrait orientation.
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            // Get the fragment manager and transaction.
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            // Replace main content with the fragment.
            AppListFragment list = new AppListFragment();
            ft.replace(R.id.main_activity_layout, list);
            ft.commit();
        }

        // Actions for 10 or more inches tablets.
        if (isTablet) {

            // Get the fragment manager and transaction.
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            // Replace list FrameLayout with the application list fragment.
            AppListFragment list = new AppListFragment();
            ft.replace(R.id.main_activity_list, list);

            // First time replace detail FrameLayout with the unselected
            // application
            // fragment.
            if (savedInstanceState == null) {
                EmptyDetailFragment empty = new EmptyDetailFragment();
                ft.replace(R.id.main_activity_detail, empty);
            }

            // Commit changes.
            ft.commit();
        }

    }

    /**
     * Overrides a superclass method. Called by the system when the device
     * configuration changes while your activity is running. Note that this will
     * only be called if you have selected configurations you would like to
     * handle with the configChanges attribute in your manifest. If any
     * configuration change occurs that is not selected to be reported by that
     * attribute, then instead of reporting it the system will stop and restart
     * the activity (to have it launched with the new configuration).
     * 
     * @param newConfig The new device configuration.
     * @see org.holoeverywhere.app.Activity#onConfigurationChanged(Configuration
     *      newConfig)
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        // No rotate on smartPhones and less than 10 inches tablets.
        if (isSmartPhone) {
            return;
        }

        super.onConfigurationChanged(newConfig);
    }

    /**
     * Interface from AppListFragment.
     * 
     * @param data The Item Selected on the applications list.
     */
    @Override
    public void onItemSelected(App app) {

        if (isSmartPhone) {

            // Launch new Activity with details.
            showDetailActivity(app);
        }

        if (isTablet) {

            // Get the fragment manager and transaction.
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            // Create the application detail fragment and replace their content.
            DetailFragment detail = DetailFragment.newInstance(app);
            ft.replace(R.id.main_activity_detail, detail);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    }

    /**
     * Private Method. Show the Detail Activity.
     * 
     * @param app Installed application on the device.
     * @see soft.xitxarello.su.data.App
     * @see android.content.Intent
     */
    private void showDetailActivity(App app) {

        // Create the Intent, add application as extras and Launch the detail
        // Activity.
        Intent detailIntent = new Intent(getApplicationContext(), DetailActivity.class);
        detailIntent.putExtra("app", app);
        startActivity(detailIntent);
    }
}
